package com.wfc.dao.daoImpl;

import com.wfc.dao.CookDao;
import com.wfc.entity.Cook;
import com.wfc.entity.Dish;
import com.wfc.util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CookDaoImpl implements CookDao {
    @Override
    public List<Cook> getCookByTitle() {
        List<Cook> newList=new ArrayList<Cook>();
        ResultSet rs=null;
        try {
            //（3）获得Statement对象，执行SQL语句
            String sql="select id,dname,mainfood,fufood,tiaofood,process,cookimg,cooktext,love from cook ";
            Object[] params={};
            rs= BaseDao.executeSQL(sql, params);
            //（4）处理执行结果(ResultSet)，
            while(rs.next()){
                int id=rs.getInt("id");
                String daname=rs.getString("dname");
                String mainfood=rs.getString("mainfood");

                String fufood=rs.getString("fufood");

                String tiaofood=rs.getString("tiaofood");

                String process=rs.getString("process");
                String cookimg=rs.getString("cookimg");
                String cooktext=rs.getString("cooktext");
                int love=rs.getInt("love");

                //封装成新闻信息对象
                Cook cook = new Cook();
                cook.setId(id);
                cook.setDname(daname);
                cook.setMainfood(mainfood);
                cook.setFufood(fufood);
                cook.setTiaofood(tiaofood);
                cook.setProcess(process);
                cook.setCooking(cookimg);
                cook.setCooktext(cooktext);
                cook.setLove(love);

                //将新闻对象放进集合中
                newList.add(cook);
            }
        }catch (SQLException e) {

            e.printStackTrace();
        }finally{
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            BaseDao.closeConnection(rs,preparedStatement,connection);
        }
        return newList;

    }

    @Override
    public boolean addCook( Cook cook) {
        boolean flag=false;
        try {
            String sql="insert into cook(dname,uname, mainfood, fufood, tiaofood,process, cooking, cooktext, love) values(?,?,?,?,?,?,?,?,?)";
            Object[] params={cook.getDname(),cook.getUname(),cook.getMainfood(),cook.getFufood(),cook.getTiaofood(),cook.getProcess(),cook.getCooking(),cook.getCooktext(),cook.getLove()};
            int i=BaseDao.update(sql, params);
            //（4）处理执行结果
            if(i>0){
                System.out.println("新增食品成功");
            }
            flag=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;

    }

    @Override
    public Cook selectBydname(String dname) {
        List<Cook> newList=new ArrayList<Cook>();
        ResultSet resultSet =null;
        Cook cook = new Cook();
        try {
            String  sql = "select * from cook where dname=? ";
            Object[] obj={dname};
            resultSet = BaseDao.executeSQL(sql, obj);
            while(resultSet.next()) {
                int id=resultSet.getInt("id");
                String daname=resultSet.getString("dname");
                String uname = resultSet.getString("uname");
                String mainfood=resultSet.getString("mainfood");

                String fufood=resultSet.getString("fufood");

                String tiaofood=resultSet.getString("tiaofood");

                String process=resultSet.getString("process");
                String cookimg=resultSet.getString("cooking");
                String cooktext=resultSet.getString("cooktext");
                int love=resultSet.getInt("love");


                cook.setId(id);
                cook.setDname(daname);
                cook.setUname(uname);
                cook.setMainfood(mainfood);
                cook.setFufood(fufood);
                cook.setTiaofood(tiaofood);
                cook.setProcess(process);
                cook.setCooking(cookimg);
                cook.setCooktext(cooktext);
                cook.setLove(love);
                newList.add(cook);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            BaseDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return cook;
    }

    @Override
    public int update(Cook cook) {
            int rows = 0;
            String sql = "UPDATE `cook` SET dname=?,uname=? ,mainfood=? ,fufood=?,tiaofood=?,process=?,cooking=?,cooktext=?,love=? WHERE id=?";
            Object[] obj = {cook};
            rows = BaseDao.cookUpdate(sql, obj);
            return rows;
    }

    @Override
    public Cook selectByimg(String cooking) {
        List<Cook> newList=new ArrayList<Cook>();
        ResultSet resultSet =null;
        Cook cook = new Cook();
        try {
            String  sql = "select id,dname,mainfood,fufood,tiaofood,process,cooking,cooktext,love from cook where cooking=? ";
            Object[] obj={cooking};
            resultSet = BaseDao.executeSQL(sql, obj);
            while(resultSet.next()) {
                int id=resultSet.getInt("id");
                String daname=resultSet.getString("dname");
                String mainfood=resultSet.getString("mainfood");
                String fufood=resultSet.getString("fufood");
                String tiaofood=resultSet.getString("tiaofood");
                String process=resultSet.getString("process");
                String cookimg=resultSet.getString("cooking");
                String cooktext=resultSet.getString("cooktext");
                int love=resultSet.getInt("love");


                cook.setId(id);
                cook.setDname(daname);
                cook.setMainfood(mainfood);
                cook.setFufood(fufood);
                cook.setTiaofood(tiaofood);
                cook.setProcess(process);
                cook.setCooking(cooking);
                cook.setCooktext(cooktext);
                cook.setLove(love);
                newList.add(cook);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            BaseDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return cook;
    }

    @Override
    public List<Cook> selectByuname(String uname) {
        List<Cook> newList=new ArrayList<Cook>();
        ResultSet resultSet =null;
        Cook cook = null;
        try {
            String  sql = "select * from cook where uname=? ";
            Object[] obj={uname};
            resultSet = BaseDao.executeSQL(sql, obj);
            while(resultSet.next()) {
                cook = new Cook();
                int id=resultSet.getInt("id");
                String daname=resultSet.getString("dname");
                String uname1=resultSet.getString("uname");
                int love=resultSet.getInt("love");
                String mainfood=resultSet.getString("mainfood");
                String fufood=resultSet.getString("fufood");
                String tiaofood=resultSet.getString("tiaofood");
                String process=resultSet.getString("process");
                String cooking=resultSet.getString("cooking");
                String cooktext=resultSet.getString("cooktext");

                cook.setId(id);
                cook.setUname(uname);
                cook.setDname(daname);
                cook.setMainfood(mainfood);
                cook.setFufood(fufood);
                cook.setTiaofood(tiaofood);
                cook.setProcess(process);
                cook.setCooking(cooking);
                cook.setCooktext(cooktext);
                cook.setLove(love);
                newList.add(cook);

            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            BaseDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return newList;
    }

    @Override
    public Cook selectBydname1(String dname) {
        ResultSet resultSet =null;
        Cook cook = new Cook();
        try {
            String  sql = "SELECT * FROM `cook` WHERE `dname`=?;";
            Object[] obj={dname};
            resultSet = BaseDao.executeSQL(sql, obj);
            while(resultSet.next()) {
                int id=resultSet.getInt("id");
                String uname=resultSet.getString("uname");
                String daname=resultSet.getString("dname");
                String mainfood=resultSet.getString("mainfood");

                String fufood=resultSet.getString("fufood");

                String tiaofood=resultSet.getString("tiaofood");

                String process=resultSet.getString("process");
                String cookimg=resultSet.getString("cooking");
                String cooktext=resultSet.getString("cooktext");
                int love=resultSet.getInt("love");


                cook.setId(id);
                cook.setUname(uname);
                cook.setDname(daname);
                cook.setMainfood(mainfood);
                cook.setFufood(fufood);
                cook.setTiaofood(tiaofood);
                cook.setProcess(process);
                cook.setCooking(cookimg);
                cook.setCooktext(cooktext);
                cook.setLove(love);



            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return cook;
    }


}
